package org.dreamwork.jasmine2.web.controls.flow;

import org.dreamwork.jasmine2.events.EventException;
import org.dreamwork.jasmine2.web.IWebControl;
import org.dreamwork.jasmine2.web.controls.WebContainer;
import org.dreamwork.util.StringUtil;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;

/**
 * Created by seth.yang on 2016/9/19
 */
public class IfControl extends WebContainer {
    private String test;
    private boolean state = false;

    @Override
    public void render (PrintWriter writer) throws IOException, EventException {
        if (state) {
            for (int i = 0; i < getChildControls ().count (); i ++) {
                this.childControls.getControl (i).render (writer);
            }
        }
    }

    @Override
    public IWebControl copy () {
        try {
            IfControl ic = (IfControl) baseCopy ();
            ic.dynamicAttributes.putAll (dynamicAttributes);
            for (String key : attrs.stringPropertyNames ()) {
                ic.attrs.setProperty (key, attrs.getProperty (key));
            }
            ic.page = page;
            return ic;
        } catch (Exception ex) {
            return super.copy ();
        }
    }

    @Override
    public void onDataBind (Object sender) throws EventException {
        super.onDataBind (sender);

        String expression = dynamicAttributes.get ("test");
        if (expression != null) {
            try {
                Object o = page.eval (expression);
                if (o != null) {
                    Class<?> type = o.getClass ();
                    if (type == boolean.class || type == Boolean.class) {
                        state = (Boolean) o;
                    } else if (Number.class.isAssignableFrom (type)) {
                        Number n = (Number) o;
                        state = (n.doubleValue () != 0);
                    } else if (Collection.class.isAssignableFrom (type)) {
                        state = !((Collection) o).isEmpty ();
                    } else if (Map.class.isAssignableFrom (type)) {
                        state = !((Map) o).isEmpty ();
                    } else if (type.isArray ()) {
                        int length = Array.getLength (o);
                        state = (length > 0);
                    } else {
                        String e = o.toString ();
                        try {
                            state = Boolean.parseBoolean (e);
                        } catch (Exception ex) {
//                            state = !StringUtil.isEmpty (e);
                        }
                        if (!state) {
                            state = !StringUtil.isEmpty (e);
                        }
                    }
                } else {
                    if ("true".equalsIgnoreCase (test)) {
                        state = true;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace ();
            }
        } else {
            if ("true".equalsIgnoreCase (test)) {
                state = true;
            }
        }
    }

    public String getTest () {
        return test;
    }

    public void setTest (String test) {
        this.test = test;
    }
}